#ifndef __CHESTS_H__
#define __CHESTS_H__

// Includes
#include <PA9.h>       // Include for PA_Lib

#include "Init.h"
#include "Boost.h"


//CHEST ONE - HP Stone
//---------------------------------------------
//---------------------------------------------
void chestOne()
{
	PA_StopSpriteAnim(0, avSprNo); //stop any previous sprite animation
	//make sure it hasn't been opened yet
	//AND that player is close enough to chest
	//NOTE: GetSpriteX/Y + 32/16 is used to get the center coordinate for each sprite
	if((stoneOneEquip != true) && ((PA_Distance((PA_GetSpriteX(0, avSprNo) + 32), (PA_GetSpriteY(0, avSprNo) + 32), 
			(PA_GetSpriteX(0, 6) + 16), (PA_GetSpriteY(0, 6) + 16))) <= distToChest))
	{

		//save initial button time
		buttonTime = Tick(gameClock);

		for(counter = 0; counter < chestCycle; counter++)
		{
			PA_StartSpriteAnimEx(0, 6, 1, 3, 7, 1, 1);
		}
		
		
		//make button visible
		PA_SetSpriteAnim(0, 11, 1);
		PA_SetSpriteAnim(0, 12, 2);

		
		while(touchButton == false)
		{
		//if button is touched
		//use button time to make SpriteTouched is not triggered when the
		//chest is touched (if the button appears over the chests location)
			if(((PA_SpriteTouched(11)) || (PA_SpriteTouched(12))) 
				&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
			{
			//hide button
				AS_SoundQuickPlay(ping);//sound effect
				PA_SetSpriteAnim(0, 11, 0);
				PA_SetSpriteAnim(0, 12, 0);
				
				//save current button time
				buttonTime = Tick(gameClock);
				
				for(counter = 0; counter < chestCount; counter++)
				{
				//find first empty slot
					if(slotFull[counter] == false)
					{
					//equip stone
						if(stoneOneEquip == false)
						{
						PA_CreateSprite(0, 1, (void*)Stones32x1280_Sprite, OBJ_SIZE_32X32,
									1, 1, slotX[counter], slotY[counter]);
						PA_SetSpriteAnim(0, 1, 0);
						stoneOneEquip = true;
						slotFull[counter] = true;
						}
					}
				}
				touchButton = true; //set flag to exit loop
				
				
			}
		}
		touchButton = false;	//reset for next time

		
	}
}




//CHEST TWO - Stamina Stone
//---------------------------------------------
//---------------------------------------------
void chestTwo()
{
	PA_StopSpriteAnim(0, avSprNo); //stop any previous sprite animation
	//make sure it hasn't been opened yet
	//AND that player is close enough to chest
	//NOTE: GetSpriteX/Y + 32/16 is used to get the center coordinate for each sprite
	if((stoneTwoEquip != true) && ((PA_Distance((PA_GetSpriteX(0, avSprNo) + 32), (PA_GetSpriteY(0, avSprNo) + 32), 
			(PA_GetSpriteX(0, 7) + 16), (PA_GetSpriteY(0, 7) + 16))) <= distToChest))
	{

		//save initial button time
		buttonTime = Tick(gameClock);

		for(counter = 0; counter < chestCycle; counter++)
		{
			PA_StartSpriteAnimEx(0, 7, 5, 7, 7, 1, 1);
		}
		
		
		//make button visible
		PA_SetSpriteAnim(0, 11, 3);
		PA_SetSpriteAnim(0, 12, 4);

		
		while(touchButton == false)
		{
		//if button is touched
		//use button time to make SpriteTouched is not triggered when the
		//chest is touched (if the button appears over the chests location)
			if(((PA_SpriteTouched(11)) || (PA_SpriteTouched(12))) 
				&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
			{
			//hide button
				AS_SoundQuickPlay(chainsaw);//sound effect
				PA_SetSpriteAnim(0, 11, 0);
				PA_SetSpriteAnim(0, 12, 0);
				
				//save current button time
				buttonTime = Tick(gameClock);
				
				for(counter = 0; counter < chestCount; counter++)
				{
				//find first empty slot
					if(slotFull[counter] == false)
					{
					//equip stone
						if(stoneTwoEquip == false)
						{
						PA_CreateSprite(0, 2, (void*)Stones32x1280_Sprite, OBJ_SIZE_32X32,
								1, 1, slotX[counter], slotY[counter]);
						PA_SetSpriteAnim(0, 2, 8);
						stoneTwoEquip = true;
						slotFull[counter] = true;
						}
					}
				}
				touchButton = true; //set flag to exit loop
				
				
			}
		}
		touchButton = false;	//reset for next time
		
	}
}





//CHEST THREE - Attack Stone
//---------------------------------------------
//---------------------------------------------
void chestThree()
{
	PA_StopSpriteAnim(0, avSprNo); //stop any previous sprite animation
	//make sure it hasn't been opened yet
	//AND that player is close enough to chest
	//NOTE: GetSpriteX/Y + 32/16 is used to get the center coordinate for each sprite
	if((stoneThreeEquip != true) && ((PA_Distance((PA_GetSpriteX(0, avSprNo) + 32), (PA_GetSpriteY(0, avSprNo) + 32), 
			(PA_GetSpriteX(0, 8) + 16), (PA_GetSpriteY(0, 8) + 16))) <= distToChest))
	{
		
		//save initial button time
		buttonTime = Tick(gameClock);
		
		for(counter = 0; counter < chestCycle; counter++)
		{
			PA_StartSpriteAnimEx(0, 8, 1, 3, 7, 1, 1);
		}
		
		
		//make button visible
		PA_SetSpriteAnim(0, 11, 5);
		PA_SetSpriteAnim(0, 12, 6);

		
		while(touchButton == false)
		{
		//if button is touched
		//use button time to make SpriteTouched is not triggered when the
		//chest is touched (if the button appears over the chests location)
			if(((PA_SpriteTouched(11)) || (PA_SpriteTouched(12))) 
				&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
			{
			//hide button
				PA_SetSpriteAnim(0, 11, 0);
				PA_SetSpriteAnim(0, 12, 0);
				
				//save current button time
				buttonTime = Tick(gameClock);
				
				for(counter = 0; counter < chestCount; counter++)
				{
				//find first empty slot
					if(slotFull[counter] == false)
					{
					//equip stone
						if(stoneThreeEquip == false)
						{
						PA_CreateSprite(0, 3, (void*)Stones32x1280_Sprite, OBJ_SIZE_32X32,
						1, 1, slotX[counter], slotY[counter]);
						PA_SetSpriteAnim(0, 3, 16);
						stoneThreeEquip = true;
						slotFull[counter] = true;
						}
					}
				}
				touchButton = true; //set flag to exit loop
				
				
			}
		}
		touchButton = false;	//reset for next time
		
	}
}

	
	
	
	
//CHEST FOUR - Defense Stone
//---------------------------------------------
//---------------------------------------------
void chestFour()
{
	PA_StopSpriteAnim(0, avSprNo); //stop any previous sprite animation
	//make sure it hasn't been opened yet
	//AND that player is close enough to chest
	//NOTE: GetSpriteX/Y + 32/16 is used to get the center coordinate for each sprite
	if((stoneFourEquip != true) && ((PA_Distance((PA_GetSpriteX(0, avSprNo) + 32), (PA_GetSpriteY(0, avSprNo) + 32), 
			(PA_GetSpriteX(0, 9) + 16), (PA_GetSpriteY(0, 9) + 16))) <= distToChest))
	{
		
		//save initial button time
		buttonTime = Tick(gameClock);
		
		for(counter = 0; counter < chestCycle; counter++)
		{
			PA_StartSpriteAnimEx(0, 9, 5, 7, 7, 1, 1);	
		}
		
		
		//make button visible
		PA_SetSpriteAnim(0, 11, 7);
		PA_SetSpriteAnim(0, 12, 8);

		
		while(touchButton == false)
		{
		//if button is touched
		//use button time to make SpriteTouched is not triggered when the
		//chest is touched (if the button appears over the chests location)
			if(((PA_SpriteTouched(11)) || (PA_SpriteTouched(12))) 
				&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
			{
			//hide button
				PA_SetSpriteAnim(0, 11, 0);
				PA_SetSpriteAnim(0, 12, 0);
				
				//save current button time
				buttonTime = Tick(gameClock);
				
				for(counter = 0; counter < chestCount; counter++)
				{
				//find first empty slot
					if(slotFull[counter] == false)
					{
					//equip stone
						if(stoneFourEquip == false)
						{
						PA_CreateSprite(0, 4, (void*)Stones32x1280_Sprite, OBJ_SIZE_32X32,
								1, 1, slotX[counter], slotY[counter]);
						PA_SetSpriteAnim(0, 4, 24);
						stoneFourEquip = true;
						slotFull[counter] = true;
						}
					}
				}
				touchButton = true; //set flag to exit loop
				
				
			}
		}
		touchButton = false;	//reset for next time
	}
}



//CHEST FIVE - Special Stone
//---------------------------------------------
//---------------------------------------------
void chestFive()
{
	PA_StopSpriteAnim(0, avSprNo); //stop any previous sprite animation
	//make sure it hasn't been opened yet
	//AND that player is close enough to chest
	//NOTE: GetSpriteX/Y + 32/16 is used to get the center coordinate for each sprite
	if((stoneFiveEquip != true) && ((PA_Distance((PA_GetSpriteX(0, avSprNo) + 32), (PA_GetSpriteY(0, avSprNo) + 32), 
			(PA_GetSpriteX(0, 10) + 16), (PA_GetSpriteY(0, 10) + 16))) <= distToChest))
	{
		//save initial button time
		buttonTime = Tick(gameClock);
		
		for(counter = 0; counter < chestCycle; counter++)
		{
			PA_StartSpriteAnimEx(0, 10, 1, 3, 7, 1, 1);
		}
		
		
		//make button visible
		PA_SetSpriteAnim(0, 11, 9);
		PA_SetSpriteAnim(0, 12, 10);

		
		while(touchButton == false)
		{
		//if button is touched
		//use button time to make SpriteTouched is not triggered when the
		//chest is touched (if the button appears over the chests location)
			if(((PA_SpriteTouched(11)) || (PA_SpriteTouched(12))) 
				&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
			{
			//hide button
				PA_SetSpriteAnim(0, 11, 0);
				PA_SetSpriteAnim(0, 12, 0);
				
				//save current button time
				buttonTime = Tick(gameClock);
				
				for(counter = 0; counter < chestCount; counter++)
				{
				//find first empty slot
					if(slotFull[counter] == false)
					{
					//equip stone
						if(stoneFiveEquip == false)
						{
						PA_CreateSprite(0, 5, (void*)Stones32x1280_Sprite, OBJ_SIZE_32X32,
									1, 1, slotX[counter], slotY[counter]);
						PA_SetSpriteAnim(0, 5, 32);
						stoneFiveEquip = true;
						slotFull[counter] = true;
						}
					}
				}
				touchButton = true; //set flag to exit loop
				
				
			}
		}
		touchButton = false;	//reset for next time
		
	}
}

#endif  //__CHESTS_H__
